using System;
using ConfigurationSettings;
using ServiceLocator;

namespace CustomControls.UserControls
{
    public partial class UserControlGeneral : UserControlBase
    {
        private IApplicationSettings _localSettings;

        public UserControlGeneral()
        {
            InitializeComponent();
            _localSettings = Locator.Resolve<IApplicationSettings>();
        }

        public override void Initialize()
        {

            //_configSettings = new ApplicationSettings();

            //Set the AskBeforeDelete flag
            checkBoxConfirmDelete.Checked = _localSettings.AskBeforeDelete;

            //Set the Sort dropdowns
            //Check the config file for the left side
            switch (_localSettings.LeftDefaultSortColumn)
            {
                case Column.None:
                    comboBoxLeftSortColumn.Text = @"(None)";
                    break;
                case Column.Name:
                    comboBoxLeftSortColumn.Text = @"Name";
                    break;
                case Column.Size:
                    comboBoxLeftSortColumn.Text = @"Size";
                    break;
                case Column.Date:
                    comboBoxLeftSortColumn.Text = @"Date";
                    break;
                default:
                    comboBoxLeftSortColumn.Text = @"(None)";
                    break;
            }

            //Check the config file for the left side
            switch (_localSettings.RightDefaultSortColumn)
            {
                case Column.None:
                    comboBoxRightSortColumn.Text = @"(None)";
                    break;
                case Column.Name:
                    comboBoxRightSortColumn.Text = @"Name";
                    break;
                case Column.Size:
                    comboBoxRightSortColumn.Text = @"Size";
                    break;
                case Column.Date:
                    comboBoxRightSortColumn.Text = @"Date";
                    break;
                default:
                    comboBoxRightSortColumn.Text = @"(None)";
                    break;
            }

            switch (_localSettings.SizeUnit)
            {
                case SizeUnitType.Bytes:
                    comboBoxSizeUnit.SelectedItem = "bytes";
                    break;
                case SizeUnitType.Kilobytes:
                    comboBoxSizeUnit.SelectedItem = "Kilobytes";
                    break;
                case SizeUnitType.Megabytes:
                    comboBoxSizeUnit.SelectedItem = "Megabytes";
                    break;
                case SizeUnitType.Gigabytes:
                    comboBoxSizeUnit.SelectedItem = "Gigabytes";
                    break;
                default:
                    comboBoxSizeUnit.SelectedItem = "bytes";
                    break;
            }

            textBoxPrecision.Text = _localSettings.SizePrecision.ToString();

            DirtyFlag = false;
        }

        private void EditValue(object argSender, EventArgs argEventArgs)
        {
            DirtyFlag = true;
        }

        public override void OnApply()
        {
            int sizePrecision;

            if (!DirtyFlag)
            {
                return;
            }

            //Set AskBeforeDelete Flag
            _localSettings.AskBeforeDelete = checkBoxConfirmDelete.Checked;

            //Set the default sort orders
            switch (comboBoxLeftSortColumn.Text)
            {
                case "Name":
                    _localSettings.LeftDefaultSortColumn = Column.Name;
                    break;
                case "Size":
                    _localSettings.LeftDefaultSortColumn = Column.Size;
                    break;
                case "Date":
                    _localSettings.LeftDefaultSortColumn = Column.Date;
                    break;
                default:
                    _localSettings.LeftDefaultSortColumn = Column.None;
                    break;
            }

            switch (comboBoxRightSortColumn.Text)
            {
                case "Name":
                    _localSettings.RightDefaultSortColumn = Column.Name;
                    break;
                case "Size":
                    _localSettings.RightDefaultSortColumn = Column.Size;
                    break;
                case "Date":
                    _localSettings.RightDefaultSortColumn = Column.Date;
                    break;
                default:
                    _localSettings.RightDefaultSortColumn = Column.None;
                    break;
            }

            switch (comboBoxSizeUnit.Text)
            {
                case "Kilobytes":
                    _localSettings.SizeUnit = SizeUnitType.Kilobytes;
                    break;
                case "Megabytes":
                    _localSettings.SizeUnit = SizeUnitType.Megabytes;
                    break;
                case "Gigabytes":
                    _localSettings.SizeUnit = SizeUnitType.Gigabytes;
                    break;
                default:
                    _localSettings.SizeUnit = SizeUnitType.Bytes;
                    break;

            }

            if (Int32.TryParse(textBoxPrecision.Text, out sizePrecision))
            {
                sizePrecision = Math.Min(sizePrecision, 5);
                _localSettings.SizePrecision = sizePrecision;
            }
            else
            {
                _localSettings.SizePrecision = 2;
            }

            _localSettings.Save();
            DirtyFlag = false;
        }

        private void textBoxPrecision_TextChanged(object argSender, EventArgs argEventArgs)
        {
            DirtyFlag = true;
        }

    }
}
